<template>
  <a-select :placeholder="placeholder"
            :allowClean="allowClean"
            :disabled="disabled" defaultValue=""
            :showSearch="true"
            :value="stateValue"
            :options="dictOptions"
            optionFilterProp="title"
            @change="handleInput">
  </a-select>
</template>

<script>
  import {getAction} from '../../api/manage';
  import {sysDictUrl} from '../../api/url'
  // 下拉单选
  export default {
    name: "AkSelectTag",
    props: {
      url: String,
      placeholder: String,
      allowClean:{
        type: Boolean,
        required:false,
        default:true
      },
      defaultFirst:{
        type:Boolean,
        required:false,
        default:false,
      },
      value:{
        type: String,
        required: false
      },
      disabled:{
        type:Boolean,
        required:false,
        default:false,
      }
    },
    data() {
      return {
        dictOptions: [],
        stateValue:''

      }
    },
    created() {
      //获取字典数据
      this.initDictData();
      this.stateValue = this.value;
    },
    mounted(){

    },
    methods: {

      initDictData(url) {
        if(!url){
          url = this.url;
        }
        if(url){
          getAction(this.url).then((res) => {

            if (res.success) {
              this.dictOptions = [
                {key:"", value:"", title:"请选择"},
                ...res.result.map(t=>
                  {return {key:t.value,value:t.value,title:t.text, data: t}})
              ];
              if(this.defaultFirst && !this.stateValue){
                this.stateValue=(this.dictOptions[1]||{}).value;
                this.handleInput(this.stateValue);
              }
            }else {
              this.dictOptions = [{key:"", value:"", title:"请选择"}]
            }
          });
        }

      },
      handleInput(e, option) {
        this.stateValue = e;
        console.log(e, option)
        this.$emit('change', e, option);
      },
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    watch:{
      value: function value(val) {
        this.stateValue = val;
      },
      url:function (val) {
        this.initDictData(val);
      }
    }
  }
</script>

<style scoped>
</style>
